/*
 * types.h
 *
 *  Created on: 2013-3-14
 *      Author: song
 */

#ifndef TYPES_H_
#define TYPES_H_
#include <netinet/in.h>

#include "stdio.h"
#include <string>

typedef enum {Heart=1,Update=2} MCmdType;
typedef enum {AP=1,STA=2} MWifiMode;

#ifdef DEBUG
#define DBG(...) printf("%s,%s,%d ",__FILE__,__FUNCTION__,__LINE__);printf("__VA_ARGS__")
#else
#define DBG(...)
#endif

#define  uint8 unsigned char
#define  uint16 unsigned short
#define  uint32 unsigned int

#define  int8  char
#define  int16  short
#define  int32  int

#define MAX_VALUE_LEN 128
#define MAX_COUNT_DEV 128


typedef struct
{
	uint32 id;
	int8 pswd[13];
}__attribute__((packed,aligned(1)))
Info_t;

typedef struct
{
	uint8 crc;
	uint8 tId;
	Info_t src;
	Info_t dst;
	uint16 cmd;
	uint32 tm;
	uint16 len;
}__attribute__((packed,aligned(1)))
Head_t;

typedef struct
{
	uint8 relay;
    uint16 cur;
}__attribute__((packed,aligned(1)))
Data_t;

typedef struct
{
	uint8 status;
	uint8 num;
    uint32 id[MAX_COUNT_DEV];
}__attribute__((packed,aligned(1)))
Dev_t;

typedef struct
{
	int8 pswd[13];
}__attribute__((packed,aligned(1)))
CHANGE_PSWD_t;

typedef struct
{
	int8 ssid[30];
	int8 pswd[30];
	int8 encryptmethod[30];
}__attribute__((packed,aligned(1)))
CONFIG_WIFI_t;

typedef struct
{
	struct sockaddr_in addr;
	uint8 cntdown;
}Addr_t;



typedef struct
{
	uint8 usrName[20];
	uint8 pswd[13];
	uint8 eMail[30];
}__attribute__((packed,aligned(1)))
RegInfo_t;





typedef struct
{
	uint32 firstId;
	uint32 lastId;
}__attribute__((packed,aligned(1)))
AdminId_t;


typedef struct
{
	std::string pswd;
}Pswd_t;



typedef struct
{
	Head_t head;
	union
	{
		uint8 status;
		Data_t data;
		Dev_t dev;
		CHANGE_PSWD_t pswd;
		AdminId_t adminid;
		RegInfo_t reg;

	};

	void SetCRC(void)
	{
		uint8 crc=0;

		uint8 *p=(uint8 *)&head;


		for(uint8 i=1;i<sizeof(Head_t);i++)
		{
			//printf("%02x ",crc);
			crc=crc^p[i];
		}

		p=(uint8 *)&status;
		for(uint16 i=0;i<head.len;i++)
		{
			//printf("%02x ",crc);
			crc=crc^p[i];
		}

		//printf("%02x \n",crc);
		head.crc=crc;
		return;

	};

	bool IsCRCCorrect(void){

		if(head.len>MAX_VALUE_LEN || head.len<0)
			return false;

		uint8 crc=0;

		uint8 *p=(uint8 *)&head;
		for(uint8 i=1;i<sizeof(Head_t);i++)
			crc=crc^p[i];

		p=(uint8 *)&status;
		for(uint16 i=0;i<head.len;i++)
			crc=crc^p[i];

		return head.crc==crc;
	}




	uint8 Decryption(uint8 b)
	{
		uint8 c=(uint8)~(b&0x55);
		b=(uint8) (b&0xaa);
		uint8 k=(uint8) ( (b&0xaa)| (c & 0x55) );
		return k;
	}

	uint8 Encryption(uint8 b)
	{
		uint8 c=(uint8)~(b&0x55);
		b=(uint8) (b&0xaa);
		uint8 k=(uint8) ( (b&0xaa)| (c & 0x55) );
		return k;
	}



}__attribute__((packed,aligned(1)))
HValue_t;




#include <boost/unordered_map.hpp>
//typedef boost::unordered_map<uint32,std::string> IdPswdMap; //设备信息表，用户信息表
typedef boost::unordered_map<uint32,Pswd_t> IdPswdMap; //设备信息表，用户信息表
typedef boost::unordered_map<uint32,Addr_t> IdAddrMap;//设备id和设备ip地址绑定表，用户id和用户ip地址绑定表
typedef boost::unordered_multimap<uint32,uint32> IdMap;//用户id和设备id绑定表

#endif /* TYPES_H_ */
